package com.yjt.sousou.user;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.yjt.sousou.Api;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.http.DialogCallback;
import com.yjt.sousou.main.MainActivity;
import com.yjt.sousou.utils.HDPreference;
import com.yjt.sousou.utils.StatusBarCompat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：83734
 * 时间：2018/11/21
 * 描述：
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.edit_sign_in_phone)
    TextInputEditText mTvName;
    @BindView(R.id.tl_sign_in_phone)
    TextInputLayout mTLName;
    @BindView(R.id.edit_sign_in_password)
    TextInputEditText mTvPassword;
    @BindView(R.id.tl_sign_in_password)
    TextInputLayout mTLPassword;
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white));
        StatusBarCompat.setLightMode(this);
    }

    @OnClick(R.id.btn_login)
    void logon(){
        final String name = mTvName.getText().toString();
        final String password = mTvPassword.getText().toString();
        if(validateAccount(name)&&validatePassword(password)){
            OkGo.<LoginEntityFirst>post(Api.get().getLoginNew())
                    .tag(this)
                    .params("mobile",name)
                    .params("true_pass",password)
                    .execute(new DialogCallback<LoginEntityFirst>(LoginEntityFirst.class,this) {
                        @Override
                        public void onSuccess(Response<LoginEntityFirst> response) {
                           LoginEntityFirst entityFirst = response.body();
                           if(entityFirst.getReturncode().equals("0")){
                               Api.get().setBASE_URL(entityFirst.getData().get(0).getServer_ip());
                               loginTrue(name,password);
                           }else {
                               showToast("登录失败");
                           }
                        }
                    });
        }
//        startActivity(MainActivity.class);
    }

    private void loginTrue(String name,String password){
        String client_id = HDPreference.getUserId(LoginTag.CLIENT_ID.name());
        OkGo.<LoginEntity>post(Api.get().getLogin())
                .tag(this)
                .params("username",name)
                .params("password",password)
                .params("client_id",client_id)
                .execute(new DialogCallback<LoginEntity>(LoginEntity.class,this) {
                    @Override
                    public void onSuccess(Response<LoginEntity> response) {
                        LoginEntity login = response.body();
                        if(login.getReturncode().equals("0")){
                            LoginEntity.DataBean dataBean = login.getData().get(0);
                            HDPreference.setUserId(LoginTag.USER_ID.name(),dataBean.getUser_id());
                            HDPreference.setUserId(LoginTag.USER_NAME.name(),dataBean.getUsername());
                            HDPreference.setUserId(LoginTag.WORK_STATE.name(),dataBean.getState_name());
                            HDPreference.setUserId(LoginTag.DEPARTMENT.name(),dataBean.getBumen_name());
                            HDPreference.setUserId(LoginTag.DEPARTMENT_ID.name(),dataBean.getBumen());
                            HDPreference.setUserId(LoginTag.DUTY.name(),dataBean.getJuese());
                            HDPreference.setUserId(LoginTag.PHONE.name(),dataBean.getDh());
                            HDPreference.setUserId(LoginTag.PERMISSION.name(),dataBean.getQx().toString());
                            AccountManager.setSingState(true);
                            startActivity(MainActivity.class);
                            finish();
                        }else {
                            showToast("登录失败"+(TextUtils.isEmpty(login.getReturnmsg())?"":login.getReturnmsg()));
                        }
                    }
                });
    }

    /**
     * 验证用户名
     */
    private boolean validateAccount(String account){
        if(StringUtils.isEmpty(account)){
            showError(mTLName,"用户名不能为空");
            return false;
        }else {
            clearError(mTLName);
            return true;
        }
    }
    /**
     * 验证密码
     */
    private boolean validatePassword(String password) {
        if (StringUtils.isEmpty(password)) {
            showError(mTLPassword,"密码不能为空");
            return false;
        }else {
            clearError(mTLPassword);
            return true;
        }
    }
    /**
     * 显示错误提示，并获取焦点
     */
    private void showError(TextInputLayout textInputLayout, String error){
        textInputLayout.setErrorEnabled(true);
        textInputLayout.setError(error);
        textInputLayout.getEditText().setFocusable(true);
        textInputLayout.getEditText().setFocusableInTouchMode(true);
        textInputLayout.getEditText().requestFocus();
    }
    private void clearError(TextInputLayout textInputLayout){
        textInputLayout.setErrorEnabled(false);
    }
}
